#include "timer.h"

void Timer_Init(TIM_TypeDef* TIMx, uint16_t ARR, uint16_t PSC)
{
    if (TIMx == TIM1)
    {
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
    }     
    else if (TIMx == TIM2)
    {
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    }
    else if (TIMx == TIM3)
    {
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    }
    else if (TIMx == TIM4)
    {
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
    } 
    else if (TIMx == TIM5)
    {
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
    } 
    else if (TIMx == TIM6)
    {
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
    } 
    else if (TIMx == TIM7)
    {
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);
    } 
    else if (TIMx == TIM8)
    {
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
    }
    else if (TIMx == TIM9)
    {      
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE);
    }  
    else if (TIMx == TIM10)
    {      
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10, ENABLE);
    }  
    else if (TIMx == TIM11) 
    {     
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM11, ENABLE);
    }  
    else if (TIMx == TIM12)
    {      
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM12, ENABLE);
    }  
    else if (TIMx == TIM13) 
    {       
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13, ENABLE);
    }
    else if (TIMx == TIM14) 
    {       
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
    }        
    else if (TIMx == TIM15)
    {
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM15, ENABLE);
    } 
    else if (TIMx == TIM16)
    {
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE);
    } 
    else
    {
        if (TIMx == TIM17)
        {
          RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17, ENABLE);
        }  
    }
    
    TIM_InternalClockConfig(TIMx);
    
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;          //时钟分频
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;      //计数器模式
    TIM_TimeBaseInitStruct.TIM_Period = ARR-1;                        //自动重装
    TIM_TimeBaseInitStruct.TIM_Prescaler = PSC-1;                      //预分频
    TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIMx, &TIM_TimeBaseInitStruct);
    
    //清除中断标志位
    TIM_ClearFlag(TIMx, TIM_FLAG_Update);
    //使能更新中断
    TIM_ITConfig(TIMx, TIM_IT_Update, ENABLE);
//    TIM_Cmd(TIMx, ENABLE);
}

void TIM_Init(void)
{
    //配置提示音
    Timer_Init(TIM3, CueTime*10, 7200);  //20ms
    //配置定时器3 NVIC
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_Init(&NVIC_InitStructure);

    //配置全局定时器
    Timer_Init(TIM2, 10000, 7200);  //1s
    //配置定时器2 NVIC
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_Init(&NVIC_InitStructure);
    TIM_Cmd(TIM2, ENABLE);
}

/*
void TIM2_IRQHandler(void)
{
    if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
    {
        
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
    }
}
*/
